# Contributor (MSYS2): David Macek <david.macek.0@gmail.com>
# Maintainer (Arch Linux): Anatol Pomozov <anatol pomozov at gmail>
# Contributor (Arch Linux): Daniel YC Lin <dlin.tw at gmail>
# Contributor (Arch Linux): Konstantin Plotnikov <kostyapl at gmail dot com>

_realname=fossil
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.15.1
pkgrel=1
pkgdesc='Simple, high-reliability, distributed software configuration management (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('BSD')
url='https://www.fossil-scm.org/'
depends=("${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-readline"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-tcl")
optdepends=("${MINGW_PACKAGE_PREFIX}-putty: SSH transport")
source=("https://www.fossil-scm.org/index.html/uv/${_realname}-src-${pkgver}.tar.gz"
        "win.patch"
        "readline.patch"
        "win-tcl-unix-path.patch")
sha256sums=('80d27923c663b2a2c710f8ae8cd549862e04f8c04285706274c34ae3c8ca17d1'
            'fb39c12fa1068dffa18713aae604c94d02eea5c1af279654a8f25c337c6517a9'
            '887c8b16de984156cb13bfdbc5715b1eff0123af0d07e6dc08265dbef0a1d519'
            '96090936b1aee3539211c400a4c8a7a9475c71f47238967a1924229798a2910b')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/win.patch"
  patch -p0 -i "${srcdir}/readline.patch"
  patch -p1 -i "${srcdir}/win-tcl-unix-path.patch"
  # this doesn't get expanded, so delete it just to be sure
  sed -i '/ENABLE_MINIZ/d' Makefile.in
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  MSYS2_ARG_CONV_EXCL=--prefix \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --with-zlib=${MINGW_PREFIX} \
    --debug \
    --disable-internal-sqlite

  make \
    CFLAGS+="-DBROKEN_MINGW_CMDLINE=not_actually_broken"
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
}
